package Mysinglelist;


import javax.print.DocFlavor;
import java.util.Objects;

public class mysinglelist {

    public static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode() {

        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        mysinglelist that = (mysinglelist) o;
        return Objects.equals(head, that.head);
    }

    @Override
    public int hashCode() {
        return Objects.hash(head);
    }

    public ListNode head;

    @Override
    public String toString() {
        return "mysinglelist{" +
                "head=" + head +
                '}';
    }

    public void creatList() {
        ListNode node1 = new ListNode(10);
        ListNode node2 = new ListNode(20);
        ListNode node3 = new ListNode(30);
        ListNode node4 = new ListNode(40);
        ListNode node5 = new ListNode(50);
        ListNode node6 = new ListNode(60);
        this.head = node1;
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;

    }

    public void show() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void show(ListNode newnode) {
        ListNode cur = newnode;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void addFirst(int val) {
        ListNode newnode = new ListNode(val);
        newnode.next = head;
        head = newnode;

    }

    public void addEnd(int val) {
        ListNode newnode = new ListNode(val);
        ListNode cur = head;
        if (cur == null) {
            head = newnode;
            return;
        }
        //注意这里的判断条件，是要找到链表的最后一个结点，并不是像遍历一样
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = newnode;
    }

    public void addIndex(int index, int val) {
        if (index < 0 || index > size()) {
            System.out.println("位置不合法");
            return;
        }
        //头
        if (index == 0) {
            addFirst(val);
            return;
        }
        //尾
        if (index == size()) {
            addEnd(val);
            return;
        }
//        ListNode cur=head.next;
//        for(int i=1;i<index-1;i++){
//            cur=cur.next;
//        }
        ListNode cur = findnode(index);
        ListNode newnode = new ListNode(val);
        newnode.next = cur.next;
        cur.next = newnode;
    }

    //找到index下标结点的前一个
    private ListNode findnode(int index) {
        ListNode cur = head;

        while (index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    public void remove(int index) {
        if (index < 0 || index >= size()) {
            System.out.println("位置不合法");
            return;
        }
        //删除头结点
        if (index == 0) {
            head = head.next;
            return;
        }
        //中间结点
        ListNode cur = findnode(index);
        cur.next = cur.next.next;

        //尾结点 把cur的地址域设为null
        if (index == size()) {
            cur.next = null;
        }
    }

    //删除值为key的第一个结点
    public void removekey(int key) {
        if (head == null) {
            return;
        }
        if (head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = searchprev(key);
        ListNode del = cur.next;
        cur.next = del.next;
    }

    //找到值为key的结点的上一个结点
    private ListNode searchprev(int key) {
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除值为key的所有结点
    public void removeallkey(int key) {
        if (head == null) {
            return;
        }

        //自己写的
        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == key) {
                ListNode del = cur.next;
                cur.next = del.next;
                //此时需要退回去，防止有连续两个以上的重复
            } else {
                cur = cur.next;
            }
        }
        //课上的(类似于双指针)
//        ListNode cur=head.next;
//        ListNode prev=head;
//
//        while(cur!=null){
//            if(cur.val==key){
//                prev.next=cur.next;
//                cur=cur.next;
//            }
//            else{
//                prev=cur;
//                cur=cur.next;
//
//            }
//
//        }
        //这一段判断头结点必须放在最后面，才能删除干净
        if (head.val == key) {
            head = head.next;
        }
    }

    public void clear() {
        this.head = null;
    }

    //反转链表（头插法）
    public ListNode reverseList() {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }
        //自己写的
//        ListNode prev=head;
//        ListNode cur=head.next;
//        for (int i=0;i<size()-1;i++) {
//            prev.next=cur.next;
//            cur.next=head;
//            head=cur;
//            cur=prev.next;
//        }
//      上课的
        ListNode cur = head;
        ListNode curNext = head.next;
//        for (int i=0;i<size()-1;i++) {
//            cur.next=curNext.next;
//            curNext.next=head;
//            head=curNext;
//            curNext=cur.next;
//        }
        while (curNext != null) {
            cur.next = curNext.next;
            curNext.next = head;
            head = curNext;
            curNext = cur.next;
        }
        return head;
    }

    //获取中间节点
    public ListNode getmidnode() {
        ListNode fast = head;
        ListNode slow = head;
        //注意这里判断条件的顺序：( fast.next != null && fast != null )
        //如果为偶数的时候就会发生空指针异常
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;

    }

    //获取链表倒数第k个结点
    public ListNode getrevknode(int k) {
        if (k <= 0) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        //让fast先走k-1步
        for (int i = 1; i <= k - 1; i++) {
            fast = fast.next;
            if (fast == null) {
                return null;
            }
        }
        //再让fast和slow同时同频走
        while (fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;

    }

    //合并两个有序链表
    public ListNode merge(mysinglelist listA, mysinglelist listB) {
        ListNode newHead = new mysinglelist.ListNode();
        ListNode currentHead = new mysinglelist.ListNode();
        if (listA.head.val >= listB.head.val) {
            newHead = listB.head;
            currentHead = listB.head;
        } else {
            newHead = listA.head;
            currentHead = listA.head;
        }
        while (listB.head != null && listA.head != null) {
            if (listA.head.val >= listB.head.val) {
                currentHead.next = listB.head;
                currentHead = currentHead.next;
                listB.head = listB.head.next;
            } else {
                currentHead.next = listA.head;
                currentHead = currentHead.next;
                listA.head = listA.head.next;
            }
        }
        if (listA.head != null) {
            currentHead.next = listA.head;
        } else {
            currentHead.next = listB.head;

        }
        return newHead;

    }

    public ListNode partition(ListNode pHead, int x) {
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = pHead;
        while (cur != null) {
            if (cur.val < x) {
                //第一次
                if (bs == null) {
                    bs = be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                //第一次
                if (as == null) {
                    as = ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        //第一段没有数据
        if (be == null) {
            return as;
        }
        be.next = as;
        //防止最大的数据不在最后一个
        if (as != null) {
            ae.next = null;
        }
        return bs;

    }

}
